var InjectManager = function()
{
	this.scriptCount = 0;
	this.styleCount = 0;

	this.ScriptLoadedCallback = function(e)
	{
		var injector = null;
		if (e.target != null)
		{
			injector = e.target.injector
		}
		else
			if (e.srcElement != null)
			{
				injector = e.srcElement.injector
			}

		injector.scriptCount--;

		if (e.target != null && e.target.callback != null)
		{

			e.target.callback();
		}
		else
			if (e.srcElement != null
					&& (e.srcElement.readyState == 'complete' || e.srcElement.readyState == 'loaded')
					&& e.srcElement.callback != null)
			{
				e.srcElement.callback();

			}

			//execute only once for collection
			else
				if (injector.scriptCount == 0
						&& injector.ScriptCallback != null)
				{

					injector.ScriptCallback();
					injector.ScriptCallback = null;

				}

	}

	this.InjectScript = function(src, callback)
	{
		if (src.constructor == Array)
		{
			this.scriptCount = src.length;
			this.ScriptCallback = callback;
			for (i = 0; i < src.length; i++)
			{
				this.InjectScriptSingle(src[i], null)
			}
		}
		else
		{
			this.scriptCount++;
			this.InjectScriptSingle(src, callback)
		}
	}

	this.InjectScriptSingle = function(src, callback)
	{
		var script = document.createElement("SCRIPT");
		script.src = src;

		script.callback = callback;
		script.injector = this;

		if (script.attachEvent)
		{
			script.attachEvent('onreadystatechange', this.ScriptLoadedCallback);
		}
		else
		{
			script.addEventListener('load', this.ScriptLoadedCallback, true);
		}
		document.getElementsByTagName('HEAD')[0].appendChild(script);
	}

	this.InjectStyle = function(src, callback)
	{
		if (src.constructor == Array)
		{
			this.styleCount = src.length;
			this.ScriptCallback = callback
			for (i = 0; i < src.length; i++)
			{
				this.InjectStyleSingle(src[i])
			}
		}
		else
		{
			this.styleCount++;
			this.InjectStyleSingle(src, callback)
		}
	}

	this.InjectStyleSingle = function(src, callback)
	{
		var style = document.createElement("link");
		style.rel = "stylesheet";
		style.href = src;
		style.callback = callback;
		style.injector = this;

		if (style.attachEvent)
		{
			style.attachEvent('onreadystatechange', this.ScriptLoadedCallback);
		}
		else
		{
			//TODO not working - confirmed by others - investigate
			style.addEventListener('load', function()
			{
				alert("NOT WORKING");
			}, true);
		}
		document.getElementsByTagName('HEAD')[0].appendChild(style);
	}
}

//InjectManager.InjectScript('js/View.js',new function(){alert(2333)});
// InjectManager.InjectScript('js/Controller.js',new function(){alert(1)});
// InjectManager.InjectScript('js/View.js',null).InjectScript('js/Controller.js',
// function(){alert(33)});

// InjectManager.InjectScript(['js/View.js','js/Controller.js'],function(){alert(Controller.constructor)});
// InjectManager.InjectScript(['js/View.js','js/Controller.js'],function(){alert(12)});
// InjectManager.InjectScript('js/View.js',function(){alert(View.constructor)});

// TODO watch/unwatch
//
// function Test_InjectScript()
// {
// alert(Test_InjectScript)
// }
//
// Test_InjectScript();

// InjectManager.InjectStyle('css.css', function(){alert(122);})

